package com.gitee.kobe577590.validator.rules.string;



import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.gitee.kobe577590.validator.AbstractRule;
import com.gitee.kobe577590.validator.exception.ValidateException;

public class StringMustBeEmail extends AbstractRule {
//    private String rex = "^(\\w+((-\\w+)|(\\.\\w+))*)\\+\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    private String rex = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";

    public StringMustBeEmail(String v, String msg) {

        this.value = v;
        this.msg = msg;
    }

    @Override
    public boolean validate() throws ValidateException {
        String v = StringUtils.trim((String) this.value);
        Pattern pattern = Pattern.compile(this.rex);
        Matcher matcher = pattern.matcher(v);
        if (!matcher.matches()) {
            throw new ValidateException(this.msg);
        }
        return true;
    }
}
